/**
 * 
 */
package org.traffometer.traffosim;

import java.lang.reflect.Constructor;

/**
 * This class instantiates and starts the user interface.
 * @author Mark Stillwell
 */
public class Main {
  
  /**
   * Default Constructor.
   */
  private Main() { // NOPMD
    // this is just here to quiet automatic code checkers.
  }
  
  /** Default User Interface. */
  private static final String defaultUiName = "CommandLine";
  
  /**
   * Instantiates and starts the user interface.
   * @param args command line arguments
   */
  public static void main(String[] args) {
    String uiName = defaultUiName;
    for (int i = 0; i < args.length - 1; i++) {
      if ("-ui".equals(args[i])) {
        uiName = args[i + 1];
        break;
      }
    }
    try {
      Class uiClass = Class.forName(Main.class.getPackage().getName() + "." + uiName + "Ui");
      Constructor uiConstructor = uiClass.getConstructor(new Class[] {args.getClass()});
      UserInterface ui = (UserInterface)uiConstructor.newInstance(new Object[] {args});
      ui.start();
    }
    catch (ClassNotFoundException e) {
      System.err.println(uiName + " user interface not found.");
    }
    catch (ClassCastException e) {
      System.err.println(uiName + " is not a user interface.");
    }
    catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
}